package com.mes.loco.aps.server.controller.wms;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.WMSService;
import com.iplant.aps.service.po.Pagination;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.aps.APSBOMItem;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.sfc.SFCBOMTaskItem;
import com.iplant.aps.service.po.wms.WMSInventoryCheck;
import com.iplant.aps.service.po.wms.WMSLineEdgeLibrary;
import com.iplant.aps.service.po.wms.WMSLineEdgeLibraryItem;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.Configuration;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.serviceimpl.CoreServiceImpl;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 线边库控制器
 *
 * @author PengYouWang
 * 2022-1-6 11:31:11
 */
@RestController
@RequestMapping("/api/WMSLineEdgeLibrary")
public class WMSLineEdgeLibraryController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(WMSLineEdgeLibraryController.class);

    final
    WMSService wWMSService;

    public WMSLineEdgeLibraryController(WMSService wWMSService) {
        this.wWMSService = wWMSService;
    }

    /**
     * 条件查询线边库存
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wSourceType = StringUtils.parseInt(request.getParameter("SourceType"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            String wBatchNumber = StringUtils.parseString(request.getParameter("BatchNumber"));

            ServiceResult<List<WMSLineEdgeLibrary>> wServiceResult = wWMSService
                    .WMS_QueryLineEdgeLibraryList(wLoginUser, wMaterial, wStartTime, wEndTime, wSourceType, wAreaID,
                            wBatchNumber);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询线边库详情
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wWMSLineEdgeID = StringUtils.parseInt(request.getParameter("WMSLineEdgeID"));

            if (wWMSLineEdgeID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<WMSLineEdgeLibrary> wServiceResult = wWMSService.WMS_QueryLineEdgeLibrary(wLoginUser,
                    wWMSLineEdgeID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 模拟报工
     */
    @GetMapping("/Report")
    public Object Report(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wItemID = StringUtils.parseInt(request.getParameter("ItemID"));

            if (wItemID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_TestReport(wLoginUser, wItemID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 线边库存盘点
     */
    @GetMapping("/LineInventoryCount")
    public Object LineInventoryCount(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 权限控制
            if (Configuration.readConfigString("EnableAccessControl", "config/config").equals("1")
                    && !CoreServiceImpl.getInstance().BMS_CheckPowerByAuthorityID(wLoginUser.CompanyID, wLoginUser.ID,
                            652013143, 0, 0)
                    .Info(Boolean.class)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_CODE_UNROLE);
            }

            // 获取参数
            int wLineEdgeID = StringUtils.parseInt(request.getParameter("LineEdgeID"));
            double wFQTY = StringUtils.parseDouble(request.getParameter("FQTY"));

            if (wLineEdgeID <= 0 || wFQTY < 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_LineInventoryCount(wLoginUser,
                    wLineEdgeID, wFQTY);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询线边库存盘点记录
     */
    @GetMapping("/LineInventoryList")
    public Object LineInventoryList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wLineEdgeID = StringUtils.parseInt(request.getParameter("LineEdgeID"));

            if (wLineEdgeID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<WMSInventoryCheck>> wServiceResult = wWMSService.WMS_QueryLineInventoryList(wLoginUser,
                    wLineEdgeID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询批量偶换件线边库数据源
     */
    @GetMapping("/BatchSourceList")
    public Object BatchSourceList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wSourceType = StringUtils.parseInt(request.getParameter("SourceType"));

            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));

            if (wPartID <= 0 || wSourceType <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<WMSLineEdgeLibrary>> wServiceResult = wWMSService.WMS_BatchSourceList(wLoginUser,
                    wPartID, wSourceType, wProductID, wLineID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量偶换件线边库消耗
     */
    @PostMapping("/CovaUse")
    public Object CovaUse(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<SFCBOMTaskItem> wSFCBOMTaskItemList = CloneTool.CloneArray(wParam.get("SFCBOMTaskItemList"),
                    SFCBOMTaskItem.class);
            List<APSBOMItem> wAPSBOMItemList = CloneTool.CloneArray(wParam.get("APSBomItemList"), APSBOMItem.class);
            if (wSFCBOMTaskItemList == null || wSFCBOMTaskItemList.size() <= 0 || wAPSBOMItemList == null || wAPSBOMItemList.size() <= 0) {
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_LineEdgeCovaUse(wLoginUser, wSFCBOMTaskItemList,
                    wAPSBOMItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * ERP过账
     */
    @GetMapping("/ERPUse")
    public Object ERPUse(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wAPSBOMID = StringUtils.parseInt(request.getParameter("APSBOMID"));
            int wSFCBOMTaskID = StringUtils.parseInt(request.getParameter("SFCBOMTaskID"));
            int wSFCBOMTaskItemID = StringUtils.parseInt(request.getParameter("SFCBOMTaskItemID"));
            int wWMSLineEdgeLibraryID = StringUtils.parseInt(request.getParameter("WMSLineEdgeLibraryID"));

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_TestERPUse(wLoginUser,
                    wAPSBOMID, wSFCBOMTaskID, wSFCBOMTaskItemID, wWMSLineEdgeLibraryID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 反冲料消耗
     */
    @GetMapping("/RecoilMaterialUse")
    public Object RecoilMaterialUse(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wWMSPickDemandItemID = StringUtils.parseInt(request.getParameter("WMSPickDemandItemID"));

            if (wWMSPickDemandItemID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_RecoilMaterialUse(wLoginUser,
                    wWMSPickDemandItemID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 维护来源单号(内部使用)
     */
    @GetMapping("/UpdateSourceID")
    public Object UpdateSourceID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_UpdateSourceID(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 维护台车BOMID
     */
    @GetMapping("/UpdateAPSBOMItemID")
    public Object UpdateAPSBOMItemID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_UpdateAPSBOMItemID(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 反冲物料手动消耗(内部使用)
     */
    @GetMapping("/RecoilUseUpdate")
    public Object RecoilUseUpdate(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wWMSLineEdgeLibraryID = StringUtils.parseInt(request.getParameter("WMSLineEdgeLibraryID"));

            if (wWMSLineEdgeLibraryID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_RecoilUseUpdate(wLoginUser, wWMSLineEdgeLibraryID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 反冲物料重新过账
     */
    @PostMapping("/RePostTheBackFlushedMaterials")
    public Object RePostTheBackFlushedMaterials(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<WMSLineEdgeLibraryItem> wWMSLineEdgeLibraryItemList = CloneTool.CloneArray(wParam.get(
                    "WMSLineEdgeLibraryItemList"), WMSLineEdgeLibraryItem.class);
            if (wWMSLineEdgeLibraryItemList == null || wWMSLineEdgeLibraryItemList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_RePostTheBackFlushedMaterials(wLoginUser,
                    wWMSLineEdgeLibraryItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 暂未推送数据一起过账
     */
    @GetMapping("/RePostAll")
    public Object RePostAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_RePostAll(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取数量匹配不上的线边物料ID集合
     */
    @GetMapping("/QueryErrorIDs")
    public Object QueryErrorIDs(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<String> wServiceResult = wWMSService.WMS_QueryErrorIDs(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 自动校对库存数据
     */
    @GetMapping("/AutoProofread")
    public Object AutoProofread(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_AutoProofread(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 自动更新总配送数
     */
    @GetMapping("/UpdateTotal")
    public Object v(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_UpdateTotal(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查询线边库使用明细
     */
    @GetMapping("/QueryUseList")
    public Object QueryUseList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            String wOrderIDs = StringUtils.parseString(request.getParameter("OrderIDs"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            String wBatchNumber = StringUtils.parseString(request.getParameter("BatchNumber"));
            int wIsPost = StringUtils.parseInt(request.getParameter("IsPost"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            String wPostMsgLike = StringUtils.parseString(request.getParameter("PostMsgLike"));

            wOrderIDs = wOrderIDs.replace("-1", "");

            int wPageIndex = StringUtils.parseInt(request.getParameter("PageIndex"));
            int wPageSize = StringUtils.parseInt(request.getParameter("PageSize"));
            String wSort = StringUtils.parseString(request.getParameter("Sort"));
            String wSortType = StringUtils.parseString(request.getParameter("SortType"));

            wPageIndex += 1;
            Pagination wPagination = Pagination.Create(wPageIndex, wPageSize, wSort, wSortType);

            ServiceResult<List<WMSLineEdgeLibraryItem>> wServiceResult = wWMSService.WMS_QueryUseList(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderIDs, wPartID, wPartPointID, wMaterial, wAreaID, wIsPost,
                    wBatchNumber, wPostMsgLike, wStartTime, wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 导出线边库使用明细
     */
    @GetMapping("/ExportUseList")
    public Object ExportUseList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            String wOrderIDs = StringUtils.parseString(request.getParameter("OrderIDs"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            String wBatchNumber = StringUtils.parseString(request.getParameter("BatchNumber"));
            int wIsPost = StringUtils.parseInt(request.getParameter("IsPost"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            String wPostMsgLike = StringUtils.parseString(request.getParameter("PostMsgLike"));

            wOrderIDs = wOrderIDs.replace("-1", "");

            ServiceResult<String> wServiceResult = wWMSService.WMS_ExportUseList(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderIDs, wPartID, wPartPointID, wMaterial, wAreaID, wIsPost,
                    wBatchNumber, wStartTime, wEndTime, wPostMsgLike);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量预留过账（选择数据）
     */
    @PostMapping("/BatchReservedPosting")
    public Object BatchReservedPosting(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            List<WMSLineEdgeLibraryItem> wWMSLineEdgeLibraryItemList = CloneTool.CloneArray(wParam.get("data"),
                    WMSLineEdgeLibraryItem.class);
            if (wWMSLineEdgeLibraryItemList == null || wWMSLineEdgeLibraryItemList.size() <= 0 || wWMSLineEdgeLibraryItemList.stream().anyMatch(p -> p.ID <= 0 || wWMSLineEdgeLibraryItemList.stream().anyMatch(q -> q.IsPost == 1))) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_BatchReservedPosting(wLoginUser,
                    wWMSLineEdgeLibraryItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 批量预留过账（查询条件）
     */
    @GetMapping("/BatchReservedPostingByCondition")
    public Object BatchReservedPostingByCondition(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wProductID = StringUtils.parseInt(request.getParameter("ProductID"));
            int wLineID = StringUtils.parseInt(request.getParameter("LineID"));
            int wCustomerID = StringUtils.parseInt(request.getParameter("CustomerID"));
            String wOrderIDs = StringUtils.parseString(request.getParameter("OrderIDs"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            String wMaterial = StringUtils.parseString(request.getParameter("Material"));
            int wAreaID = StringUtils.parseInt(request.getParameter("AreaID"));
            String wBatchNumber = StringUtils.parseString(request.getParameter("BatchNumber"));
            int wIsPost = StringUtils.parseInt(request.getParameter("IsPost"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            String wPostMsgLike = StringUtils.parseString(request.getParameter("PostMsgLike"));
            int wIsPostSetValue = StringUtils.parseInt(request.getParameter("IsPostSetValue"));

            wOrderIDs = wOrderIDs.replace("-1", "");

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_BatchReservedPostingByCondition(wLoginUser,
                    wProductID, wLineID, wCustomerID, wOrderIDs, wPartID, wPartPointID, wMaterial, wAreaID, wIsPost,
                    wBatchNumber, wStartTime, wEndTime, wPostMsgLike, wIsPostSetValue);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 处理数据（反冲过账11-23 -- 12-15）
     */
    @GetMapping("/HandleData")
    public Object HandleData(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wWMSService.WMS_LineHandleData(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
